package com.jtny.nytb.config.dictionary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jtny.component.dto.CurrentUserDTO;
import com.jtny.component.entity.LocalHolder;
import com.jtny.component.enums.GenericStatusEnum;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.Assert;
import com.jtny.component.util.BeanUtils;
import com.jtny.component.util.CurrentUserUtils;
import com.jtny.nytb.config.dictionary.dao.TssoConfigDataDictDao;
import com.jtny.nytb.config.dictionary.dao.TssoConfigDataDictionaryDao;
import com.jtny.nytb.config.dictionary.domain.DictionaryDO;
import com.jtny.nytb.config.dictionary.domain.DictionaryDataDO;
import com.jtny.nytb.config.dictionary.dto.DictionaryDTO;
import com.jtny.nytb.config.dictionary.dto.DictionaryDataDTO;
import com.jtny.nytb.config.dictionary.service.DictionaryDataBiz;
import com.jtny.nytb.config.dictionary.vo.DictionaryDataVO;
import com.jtny.nytb.config.errorcode.DictionaryErrorCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 数据字典项业务实现类，具体实现DictionaryDataBiz定义的接口
 *
 * @author 智慧能源中心-lyq
 * @version 1.0
 * @package com.jtny.nytb.config.dictionary.biz.impl
 * @fileName DictionaryDataBizImpl.java
 * @createTime 2023年09月01日 10:28
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Service
public class DictionaryDataBizImpl implements DictionaryDataBiz {

    @Autowired
    private TssoConfigDataDictDao dictionaryDAO;
    @Autowired
    private TssoConfigDataDictionaryDao dictionaryDataDAO;

    /**
     * 新增数据字典项操作
     *
     * @param dictionaryDataVO 数据字典项VO对象
     * @return 数据字典项VO对象
     * @throws ServiceException 抛出异常
     */
    @Override
    public DictionaryDataVO saveDictionaryData(DictionaryDataVO dictionaryDataVO) throws ServiceException {
        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装DTO对象
        DictionaryDataDTO dictionaryDataDTO = BeanUtils.copyToObject(dictionaryDataVO, DictionaryDataDTO.class);
        CurrentUserUtils.setCreateDefaultField(dictionaryDataDTO, currentUserDTO);
        dictionaryDataDTO.setStatus(GenericStatusEnum.NORMAL.getValue());
        //保存数据字典
        //数据库操作
        DictionaryDataDTO resultDTO = save(dictionaryDataDTO);
        //将数据字典DTO对象转化为VO对象，并返回
        return BeanUtils.copyToObject(resultDTO, DictionaryDataVO.class);
    }

    /**
     * 查询数据字典项操作
     *
     * @param dictionaryDataId 数据字典项ID
     * @return 数据字典项VO对象
     * @throws ServiceException 抛出异常
     */
    @Override
    public DictionaryDataVO detailDictionaryData(Long dictionaryDataId) throws ServiceException {
        //通过数据字典id获取数据字典DTO对象
        //数据库操作
        DictionaryDataDTO dictionaryDataDTO = getById(dictionaryDataId);
        if (dictionaryDataDTO == null) {
            return null;
        } else {
            //封装数据字典信息VO对象，并返回
            return BeanUtils.copyToObject(dictionaryDataDTO, DictionaryDataVO.class);
        }
    }

    /**
     * 更新数据字典项操作
     *
     * @param dictionaryDataVO 数据字典项VO对象
     * @throws ServiceException 抛出异常
     */
    @Override
    public void updateDictionaryData(DictionaryDataVO dictionaryDataVO) throws ServiceException {
        Long dictionaryDataVOId = dictionaryDataVO.getId();
        if (dictionaryDataVOId == null) {
            throw new ServiceException(DictionaryErrorCodeEnum.PARAM_DICTIONARY_ID_IS_NULL.getErrorCode(), DictionaryErrorCodeEnum.PARAM_DICTIONARY_ID_IS_NULL.getErrorMessage());
        }
        //通过数据字典id获取数据字典DTO对象
        //数据库操作
        DictionaryDataDTO dictionaryDataDTO = getById(dictionaryDataVOId);
        if (dictionaryDataDTO == null) {
            throw new ServiceException(DictionaryErrorCodeEnum.PARAM_OBJECT_IS_NULL.getErrorCode(), DictionaryErrorCodeEnum.PARAM_OBJECT_IS_NULL.getErrorMessage());
        }
        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的数据字典DTO对象
        BeanUtils.copyObject(dictionaryDataVO, dictionaryDataDTO);
        CurrentUserUtils.setUpdateDefaultField(dictionaryDataDTO, currentUserDTO);
        //修改操作
        //数据库操作
        update(dictionaryDataDTO);
    }

    /**
     * 删除数据字典项
     *
     * @param id 数据字典项Id
     * @throws ServiceException 抛出异常
     */
    @Override
    public void deleteDictionaryData(Long id) throws ServiceException {
        //通过数据字典id获取产品DTO对象
        //数据库操作
        DictionaryDataDTO dictionaryDataDTO = getById(id);
        if (dictionaryDataDTO == null) {
            throw new ServiceException(DictionaryErrorCodeEnum.PARAM_NOT_FIND.getErrorCode(), DictionaryErrorCodeEnum.PARAM_NOT_FIND.getErrorMessage());
        }
        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的数据字典DTO对象
        dictionaryDataDTO.setStatus(GenericStatusEnum.DELETED.getValue());
        CurrentUserUtils.setUpdateDefaultField(dictionaryDataDTO, currentUserDTO);
        //修改操作
        //数据库操作
        update(dictionaryDataDTO);
    }
    //////////////

    public DictionaryDataDTO save(DictionaryDataDTO dictionaryDataDTO) throws ServiceException {
        String dictionaryCode = getDictionaryCode(dictionaryDataDTO);
        // TODO: 2024/7/15   标签
        //setLabel(dictionaryCode, dictionaryDataDTO);

        // 将DTO对象的属性复制给DO对象
        DictionaryDataDO dictionaryDataDO = BeanUtils.copyToObject(dictionaryDataDTO, DictionaryDataDO.class);
        // 保存操作
        dictionaryDataDAO.insert(dictionaryDataDO);
        // 将id设置给DTO对象
        dictionaryDataDTO.setId(dictionaryDataDO.getId());

        return dictionaryDataDTO;
    }


    public void update(DictionaryDataDTO dictionaryDataDTO) throws ServiceException {
        // 将DTO对象的属性复制给DO对象
        DictionaryDataDO dictionaryDataDO = BeanUtils.copyToObject(dictionaryDataDTO, DictionaryDataDO.class);
        // 修改操作
        dictionaryDataDAO.updateById(dictionaryDataDO);
        //如果是删除数据统一标识下的字典项，需同步标签对应状态
//        String code = dictionaryDAO.selectById(dictionaryDataDTO.getDictId()).getCode();
//        if ("unified_identifier_type".equals(code) && dictionaryDataDTO.getStatus().equals(GenericStatusEnum.DELETED.getValue())){
//            DataLabelDTO dataLabelDTO = dataLabelService.getLabelByName(dictionaryDataDTO.getDescription());
//            dataLabelDTO.setStatus(GenericStatusEnum.DELETED.getValue());
//            dataLabelService.update(dataLabelDTO);
//        }
    }

//    private void setLabel(String dictionaryCode, DictionaryDataDTO dictionaryDataDTO) {
//        if ("unified_identifier_type".equals(dictionaryCode)) {
//            DataLabelDTO dataLabelDTO = new DataLabelDTO();
//            dataLabelDTO.setLabelName(dictionaryDataDTO.getValue());
//            dataLabelDTO.setType("固定标签");
//            dataLabelDTO.setIsConfigured(1);
//            dataLabelDTO.setStatus(GenericStatusEnum.NORMAL.getValue());
//            dataLabelDTO.setDescription(dictionaryDataDTO.getDescription());
//            dataLabelService.save(dataLabelDTO);
//        }
//    }

    private String getDictionaryCode(DictionaryDataDTO dictionaryDataDTO) {
        Long dictId = dictionaryDataDTO.getDictId();
        Assert.notNull(dictId, "字典id不能为空");
        DictionaryDTO dictionaryDTO = dictionaryDAOgetById(dictId);
        Assert.notNull(dictionaryDTO, "字典不存在");
        return dictionaryDTO.getCode();
    }
    public DictionaryDTO dictionaryDAOgetById(Long id) {
        // 通过主键id查询DO对象
        DictionaryDO dictionaryDO = dictionaryDAO.selectById(id);
        return BeanUtils.copyToObject(dictionaryDO, DictionaryDTO.class);
    }


    public DictionaryDataDTO getById(Long id) {
        // 通过主键id查询DO对象
        DictionaryDataDO dictionaryDataDO = dictionaryDataDAO.selectById(id);
        return BeanUtils.copyToObject(dictionaryDataDO, DictionaryDataDTO.class);
    }


    public List<DictionaryDataDTO> getByDictionaryIds(Set<Long> ids) {
        QueryWrapper<DictionaryDataDO> wrapper = new QueryWrapper<>();
        wrapper.in("dict_id", ids);
        List<DictionaryDataDO> dictionaryDataDOS = dictionaryDataDAO.selectList(wrapper);
        if (dictionaryDataDOS != null) {
            return BeanUtils.copyToList(dictionaryDataDOS, DictionaryDataDTO.class);
        } else {
            return null;
        }
    }


    public List<DictionaryDataDTO> getByDictionaryId(Long dictionaryId) {
        QueryWrapper<DictionaryDataDO> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", dictionaryId);
        wrapper.ne("status", 9);
        List<DictionaryDataDO> dictionaryDataDOS = dictionaryDataDAO.selectList(wrapper);
        if (dictionaryDataDOS != null) {
            return BeanUtils.copyToList(dictionaryDataDOS, DictionaryDataDTO.class);
        } else {
            return null;
        }
    }


    public int saveBatch(List<DictionaryDataDTO> list) throws ServiceException {
        //DTO对象转DO对象
        List<DictionaryDataDO> dictionaryDataDOS = BeanUtils.copyToList(list, DictionaryDataDO.class);
        //批量插入并且返回
        return dictionaryDataDAO.insertBatch(dictionaryDataDOS);
    }


    public DictionaryDataDTO queryByDescriptionAndDictId(DictionaryDataDTO dictionaryDataDTO) {

        QueryWrapper<DictionaryDataDO> wrapper = new QueryWrapper<>();

        if (dictionaryDataDTO != null) {

            wrapper.eq("dict_id", dictionaryDataDTO.getDictId())
                    .eq("dict_description", dictionaryDataDTO.getDescription()).eq("status", 0);

            DictionaryDataDO dictionaryDataDO = dictionaryDataDAO.selectOne(wrapper);

            return BeanUtils.copyToObject(dictionaryDataDO, DictionaryDataDTO.class);
        } else
            return null;
    }
}
